/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package clientprogram;

import Command.CommandStatement;
import Command.Constant_Command;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 *
 * @author ASUS
 */
public class LockScreen extends Thread{
    public boolean working = false;
    public JFrame frame;
    InputManager inputManager;
    LoginForm dialog;
    Socket socket;
    LockThread lockThread = null;
    
    private static LockScreen instance = null;
    public static LockScreen Instance() throws IOException{
        if (instance == null){
            instance = new LockScreen();
        }
        return instance;
    }
    public class LockThread extends Thread{
        
        public LockThread() throws IOException
        {
            frame = new JFrame("Login");
            frame.setLocationRelativeTo(null);
            frame.setLayout(new BorderLayout());
            frame.add(new Component() {
                    BufferedImage img = ImageIO.read(new File("lockimage.jpg"));

                    @Override
                    public void paint(Graphics g) {
                        super.paint(g);
                        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
                    }
                });
            frame.setAlwaysOnTop(true);
            frame.addMouseListener(new MouseListener() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    java.awt.EventQueue.invokeLater(new Runnable() {
                        public void run() {

                            dialog.setVisible(true);
                            //dialog.toFront();
                        }
                    });
                }

                @Override
                public void mousePressed(MouseEvent e) {

                }

                @Override
                public void mouseReleased(MouseEvent e) {

                }

                @Override
                public void mouseEntered(MouseEvent e) {

                }

                @Override
                public void mouseExited(MouseEvent e) {

                }
            });
            frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            frame.setUndecorated(true);

            dialog = new LoginForm(frame, true, socket);
            dialog.setLocationRelativeTo(frame);
            dialog.setAlwaysOnTop(true);

            working = false;
            //start();
        }
        
        public void run()
        {
            //SetFullScreen();
            working = true;
            frame.setVisible(true);
            try
            {
                Robot robot = new Robot();
                while(working)
                {
                    //robot.keyRelease(KeyEvent.VK_ALT);
                    //robot.keyRelease(KeyEvent.VK_TAB);
                    frame.requestFocus();
                    Thread.sleep(10);
                }
                ClientMain.Instance().Display();
                frame.setVisible(false);
                frame.dispose();
            }
            catch(Exception e)
            {

            }
        }
    }
    
    
    
    public void Open(){
        if (!working){
            try {
                lockThread = new LockThread();
                lockThread.start();
            } catch (IOException ex) {
                Logger.getLogger(LockScreen.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
    
    public void Close(){
        if (working){
           working = false;
        }
    }
}
